<?php 
function upload_file($file_name){
	$image=$file_name;
	$result = "";
	define('UPLOAD_DIR','Images/Product/');
	
	define('MAX_FILES_SIZE',10000000);
	$file = str_replace(" ","_",$_FILES['image']['name']);
	//convert maximun size to KB
	$max = number_format(MAX_FILES_SIZE/1024,1)." KB";
	//create an array of permitted MIME types
	$premitted = array('image/gif','image/jpeg','image/pjpeg','image/png');
	//begin by assuming the file is unacceptable
	$sizeOK = false;
	$typeOK = false;
	
	//Check that file is within the permitted size
	if($_FILES['image']['size']>0 && $_FILES['image']['size']<= MAX_FILES_SIZE)
	{
		$sizeOK = true;
	}
	
	//Check that file is within the permitted size
	foreach($premitted as $type)
	{
		if($type == $_FILES['image']['type'])
		{
			$typeOK = true;
			break;
		}
	}
	
	if($sizeOK && $typeOK)
	{
		switch($_FILES['image']['error'])
		{
			case 0:
				if(!file_exists(UPLOAD_DIR.$file))
				{
					//move the file to the upload folder and rename
					$success = move_uploaded_file($_FILES['image']['tmp_name'],UPLOAD_DIR.$file);
					$add=UPLOAD_DIR.$file; // the path with the file name where the file will be stored, upload is the directory name. 
				}
				else
				{
					$now  = date("Y-m-d-His",time());
					$success = move_uploaded_file($_FILES['image']['tmp_name'],UPLOAD_DIR.$now.$file);
				}
				if($success){
					$result = "$file upload thành công";
					//$result = "$file uploaded successfully";
				}
				else{
					$result = "Lỗi upload $file.Vui long thử lại.";
					//$result = "Error uploading $file.Please try again.";
				}
				break;
			case 3:
				$result = "Lỗi upload $file.Vui long thử lại.";
				//$result = "Error uploading $file.Please try again.";
			default:
				$result = "Lỗi hệ thống upload $file.Vui long thử lại.";
				//$result = "System error uploadding $file contact webmaster";
		}
	}
	elseif($_FILES['image']['error']==4){
		$result = "Chưa chọn file upload";
		//$result = "No file selected";
	}
	else{
		$result = "Lỗi file upload. Maximum size: $max. File upload phải là gif, jpg, png.";
		//$result ="$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
	}
	return $result;
}


function CreateThumbnail($file_name)
{
	$image = $file_name;
	define('UPLOAD_DIR_THUMBNAIL','Images/Product/thumb/');
	/////////////////////////////////////////////// Starting of GIF thumbnail creation///////////
	
	///////// Start the thumbnail generation//////////////
	$n_width=192;          // Fix the width of the thumb nail images
	$n_height=32;         // Fix the height of the thumb nail imaage
	
	if ($_FILES['image']['type']=="image/gif")
	{
		$im=imagecreatefromgif($add);
		$width=imagesx($im);              // Original picture width is stored
		$height=imagesy($im);                  // Original picture height is stored
		$newimage=imagecreatetruecolor($n_width,$n_height);
		imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
		if (function_exists("imagegif"))
	 	{				
			imagegif($newimage,UPLOAD_DIR_THUMBNAIL);
		}
		elseif (function_exists("imagejpeg")) {
			imagejpeg($newimage,UPLOAD_DIR_THUMBNAIL);
		}
	chmod("$tsrc",0777);
	}////////// end of gif file thumb nail creation//////////
	
	////////////// starting of JPG thumb nail creation//////////
	if($_FILES['image']['type']=="image/pjpeg")
	{
		$im=imagecreatefromjpeg($add); 
		$width=imagesx($im);              // Original picture width is stored
		$height=imagesy($im);             // Original picture height is stored
		$newimage=imagecreatetruecolor($n_width,$n_height);                 
		imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
		imagejpeg($newimage,$tsrc);
		chmod("$tsrc",0777);
	}
	////////////////  End of JPG thumb nail creation //////////
	
	
	////////////// starting of PNG thumb nail creation//////////
	if($_FILES['image']['type']=="image/png")
	{
		$im=imagecreatefrompng(($add); 
		$width=imagesx($im);              // Original picture width is stored
		$height=imagesy($im);             // Original picture height is stored
		$newimage=imagecreatetruecolor($n_width,$n_height);                 
		imagecopyresized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
		imagepng($newimage,$tsrc);
		chmod("$tsrc",0777);
	}
	////////////////  End of PNG thumb nail creation //////////
}
?>